package com.study.seckill.web.page;

import com.study.seckill.entity.CourseSeckill;
import com.study.seckill.entity.CourseSeckillES;
import com.study.seckill.service.CourseSeckillService;
import com.study.seckill.vo.CourseSeckillDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: CourseSeckillController
 * @Author: wYin
 * @Date: 2022/3/10 14:48
 */
@Controller
@RequestMapping("/page/courseSeckill")
public class CourseSeckillController {
    @Autowired
    private CourseSeckillService courseSeckillService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * @Desc: 秒杀课程展示--可展示的课程列表
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: wYin
     * @Time: 2022/3/10 15:10
     */
    @GetMapping("/showList")
    public String showList(Model model){
//        List<CourseSeckill> list = (List<CourseSeckill>) redisTemplate.opsForValue().get("list");
//        List<CourseSeckill> courseSeckills = new ArrayList<>();
//        if(list==null){
//            courseSeckills = courseSeckillService.showCourseSeckill();
//            redisTemplate.opsForValue().set("list",courseSeckills);
//        }
        List<CourseSeckill> courseSeckills = courseSeckillService.showCourseSeckill();
//        courseSeckills.forEach(t->{
//            Date startTime = new Date(t.getStartTime());
//            if(startTime.getTime()<=new Date().getTime()){
//                t.setTimeCom(true);
//            }else{
//                t.setTimeCom(false);
//            }
//        });
//        courseSeckills = list;
        model.addAttribute("data",courseSeckills);
        return "seckill_list";
    }

    @GetMapping("/add")
    public String add(){
        return "seckill_add";
    }

    @PostMapping("/add")
    public synchronized String add(CourseSeckill courseSeckill,Model model){
        //执行秒杀课程添加
        CourseSeckill addResult = courseSeckillService.add(courseSeckill);
        if(addResult!=null) {
            model.addAttribute("msg", "成功");
        }else {
            model.addAttribute("msg", "失败");
        }
        return "seckill_add";
    }

    @GetMapping("/courseList")
    public String courseList(){
        return "seckill_course_select";
    }
    
    @GetMapping("/item/{id}")
    public String item(@PathVariable("id")Integer id, Model model) throws ParseException {
        //根据秒杀id查询
        CourseSeckillDetailVO result = courseSeckillService.getSeckillInfoById(id);
//        SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHHmmss");
//        try {
//            Date time = sim.parse(result.getCourseSeckill().getStartTime());
//            if (time.getTime()<new Date().getTime()){//可以购买
//                model.addAttribute("show",false);
//            }else{
//                model.addAttribute("show",true);
//            }
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        model.addAttribute("data",result);
        return "item";
    }

    @GetMapping("/getByName/{name}")
    public String  getByName(@PathVariable("name") String name,Model model){
        List<CourseSeckill> list = new ArrayList<>();
        List<CourseSeckillES> byName = courseSeckillService.getByName(name);
        byName.forEach(t->{
            CourseSeckill courseSeckill = new CourseSeckill();
            courseSeckill.setId(t.getId());
            courseSeckill.setName(t.getName());
            courseSeckill.setCourseId(t.getCourse_id());
            courseSeckill.setShowTime(t.getShow_time());
            courseSeckill.setStartTime(t.getStart_time());
            courseSeckill.setEndTime(t.getEnd_time());
            courseSeckill.setSeckillPrice(t.getSeckill_price());
            courseSeckill.setFavorablePrice(t.getFavorable_price());
            courseSeckill.setSeckillNum(t.getSeckill_num());
            courseSeckill.setVersion(t.getVersion());
            list.add(courseSeckill);
        });
        model.addAttribute("data",list);
        return "seckill_list";
    }


}
